﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using XLua;
[LuaCallCSharp]
public class EventTriggerListener : EventTrigger
{
    #region 【属性】

    public delegate void VoidDelegate(GameObject go);
    //public Action<GameObject>  VoidDelegate(GameObject go);
    public VoidDelegate onClick;

    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;
    public Action<GameObject, Vector2> onDrag;

    public Action<string> TestDelegate = (param) =>
    {
        Debug.Log("TestDelegate in c#:" + param);
    };

    //public Action<GameObject> TestDelegateOnDown = (go) =>
    //{
    //    //onDown(go);
    //};


    public void SetOnDown(VoidDelegate fun)
    {
        onDown = fun;
    }
    #endregion

    #region 【公有方法】

    public static EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(gameObject);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null) onEnter(gameObject);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(gameObject);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp(gameObject);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject, eventData.delta);
    }



    public void AddListenerOnClick(VoidDelegate action)
    {
        onClick = action;
    }

    public void AddListenerOnDown(VoidDelegate action)
    {
        onDown = action;
    }
    public void AddListenerOnEnter(VoidDelegate action)
    {
        onEnter = action;
    }
    public void AddListenerOnExit(VoidDelegate action)
    {
        onExit = action;
    }
    public void AddListenerOnUp(VoidDelegate action)
    {
        onUp = action;
    }
    public void AddListenerOnSelect(VoidDelegate action)
    {
        onSelect = action;
    }
    public void AddListenerOnUpdateSelected(VoidDelegate action)
    {
        onUpdateSelect = action;
    }

    public void AddListenerOnnDrag(Action<GameObject, Vector2> action)
    {
        onDrag = action;
    }
    #endregion
}
